﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Xml;
using System.Xml.Xsl;

namespace WebsiteBanHang
{
    public class XL_XML
    {
        private static string chuoiXML = "";
        private static XmlDocument taiLieu = null;

        public static string ChuoiXML
        {
            get { return XL_XML.chuoiXML; }
            set { XL_XML.chuoiXML = value; }
        }
        public static XmlDocument TaiLieu
        {
            get { return XL_XML.taiLieu; }
        }

        #region Nhom ham doc
        public static bool DocTaiLieu()
        {
            bool kq = true;
            taiLieu = new XmlDocument();

            try
            {
                taiLieu.LoadXml(chuoiXML);
            }
            catch (Exception ex)
            {
                kq = false;
                taiLieu = null;
            }

            return kq;
        }

        public static XmlNodeList DocDanhSach(string xPath)
        {
            if (taiLieu == null || xPath == null)
                return null;

            XmlNodeList danhSach;

            try
            {
                danhSach = taiLieu.SelectNodes(xPath);
            }
            catch (Exception ex)
            {
                danhSach = null;
            }

            return danhSach;
        }

        public static XmlElement DocNut(string xPath)
        {
            if (taiLieu == null || xPath == null)
                return null;

            XmlElement nut;

            try
            {
                nut = (XmlElement)taiLieu.SelectSingleNode(xPath);
            }
            catch (Exception ex)
            {
                nut = null;
            }

            return nut;
        }
        #endregion

        #region Nhom ham ghi
        public static string GhiTaiLieu()
        {
            if (taiLieu == null)
                return null;

            return taiLieu.OuterXml;
        }

        public static bool GhiDanhSach(string xPath, XmlNodeList danhSach)
        {
            if (taiLieu == null || xPath == null || danhSach == null)
                return false;

            bool kq = true;
            try
            {
                XmlNode nutCha = taiLieu.SelectSingleNode(xPath);
                for (int i = 0; i < danhSach.Count; i++)
                    nutCha.AppendChild(danhSach[i]);
            }
            catch (Exception ex)
            {
                kq = false;
            }

            return kq;
        }

        public static bool GhiDanhSach(XmlNode nutCha, XmlNodeList danhSach)
        {
            if (taiLieu == null || nutCha == null || danhSach == null)
                return false;

            bool kq = true;
            try
            {
                for (int i = 0; i < danhSach.Count; i++)
                    nutCha.AppendChild(danhSach[i]);
            }
            catch (Exception ex)
            {
                kq = false;
            }

            return kq;
        }

        public static bool GhiNut(string xPath, XmlNode nut)
        {
            if (taiLieu == null || xPath == null || nut == null)
                return false;

            bool kq = true;
            try
            {
                XmlNode nutCha = taiLieu.SelectSingleNode(xPath);
                nutCha.AppendChild(nut);
            }
            catch (Exception ex)
            {
                kq = false;
            }

            return kq;
        }

        public static bool GhiNut(XmlNode nutCha, XmlNode nut)
        {
            if (taiLieu == null || nutCha == null || nut == null)
                return false;

            bool kq = true;
            try
            {
                nutCha.AppendChild(nut);
            }
            catch (Exception ex)
            {
                kq = false;
            }

            return kq;
        }

        public static bool XoaNut(XmlNode nut)
        {
            if (taiLieu == null || nut == null)
                return false;

            bool kq = true;
            try
            {
                XmlNode nutCha = nut.ParentNode;
                nutCha.RemoveChild(nut);
            }
            catch (Exception ex)
            {
                kq = false;
            }
            return kq;
        }
        #endregion

        #region Nhóm hàm xuất
        //public static void XuatRaLuoi(string xPath, DataTable table)
        //{
        //    if (taiLieu == null || xPath == null)
        //        return;

        //    try
        //    {
        //        XmlNodeList danhSachNut = taiLieu.SelectNodes(xPath);
        //        int soThuocTinh = danhSachNut[0].Attributes.Count;
        //        if (soThuocTinh != table.Columns.Count)
        //            return;
        //        for (int i = 0; i < danhSachNut.Count; i++)
        //        {
        //            DataGridViewRow dongDuLieu = new DataGridViewRow();
        //            dongDuLieu.CreateCells(luoi);
        //            for (int j = 0; j < soThuocTinh; j++)
        //            {
        //                dongDuLieu.Cells[j].Value = danhSachNut[i].Attributes[j].Value;
        //            }
        //            luoi.Rows.Add(dongDuLieu);
        //            luoi.Rows[luoi.RowCount - 1].Tag = (XmlElement)danhSachNut[i];
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        //        return;
        //    }
        //}
        #endregion
       
    }
}